export const getBrowserName = () => {
  const explorer = window.navigator.userAgent.toLowerCase();
  let ver = 0;
  // ie
  // if (explorer.indexOf('sie') >= 0) {
  // eslint-disable-next-line
  if (!!window.ActiveXObject || "ActiveXObject" in window) {
    // var ver = explorer.match(/msie ([\d.]+)/)[1];
    return { type: "IE", version: ver };
  }
  // firefox
  if (explorer.indexOf("firefox") >= 0) {
    [, ver] = explorer.match(/firefox\/([\d.]+)/);
    return { type: "Firefox", version: ver };
  }
  // Chrome
  if (explorer.indexOf("chrome") >= 0) {
    [, ver] = explorer.match(/chrome\/([\d.]+)/);
    return { type: "Chrome", version: ver };
  }
  // Opera
  if (explorer.indexOf("opera") >= 0) {
    [, ver] = explorer.match(/opera.([\d.]+)/);
    return { type: "Opera", version: ver };
  }
  // Safari
  if (explorer.indexOf("safari") >= 0) {
    [, ver] = explorer.match(/version\/([\d.]+)/);
    return { type: "Safari", version: ver };
  }
  return {};
};

const browserInfo = getBrowserName();

export const isIE = () => {
  return browserInfo?.type === "IE";
};

export const isSafari = () => {
  return browserInfo?.type === "Safari";
};

export const isFirefox = () => {
  return browserInfo?.type === "Firefox";
};

export const arr2tree = list => {
  const res = [];
  const map = list.reduce((curRes, v) => ((curRes[v.id] = v), curRes), {});
  for (const item of list) {
    if (item.pid === 0) {
      res.push(item);
      continue;
    }
    if (item.pid in map) {
      const parent = map[item.pid];
      parent.children = parent.children || [];
      parent.children.push(item);
    }
  }
  return res;
};
